package app.view.component 
{
	import app.view.component.loading.ProgressBar;
	import com.greensock.easing.*;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.temple.core.CoreMovieClip;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author ria.go.vn
	 */
	[Embed(source="../../../../lib/loading.swf", symbol="Loading")]
	public class Loading extends CoreMovieClip 
	{
		public var symicon:MovieClip;
		public var proload:MovieClip;
		public var loading:TextField;
		public var back:MovieClip;
		private var progress:ProgressBar;
		
		public function Loading() {
			this.visible = false;
			back.visible = false;
			addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
		}
		private function onAddToStage(evt:Event):void {
			loading.text = "Loading..." + "0%";
			progress 	= new ProgressBar(proload, 200);//248
			stage.addEventListener( Event.RESIZE, onResize );	
			onResize(evt);
			createDataDelay();
		}
		public function onResize(evt:Event):void {
			
			back.width 	= stage.stageWidth;
			back.height = stage.stageHeight;
			
			x = Math.round(stage.stageWidth / 2);
			y = Math.round(stage.stageHeight / 2);
			
		}
		private function transformBackground():void {
			back.visible = true;
			TweenLite.from(back, 0.5, { alpha:0 } );
		}
		private function createDataDelay():void {
			
			symicon.visible = true;
			proload.visible = false;
			loading.visible = true;
			
			TweenMax.from(symicon, 1, { scaleX:0.2, scaleY:0.2, ease:Elastic.easeOut } );
			TweenMax.from(proload, 1, { alpha:0, y:(proload.y - 100), ease:Elastic.easeOut } );
			TweenMax.from(loading, 1, { alpha:0, y:(proload.y - 100), ease:Elastic.easeOut } );
			
		}
		public function setPercent(percent:Number):void {
			
			setShow();
			
			var a:String = "0%";
			var b:String = Math.round(Math.floor(percent)) + "%";
			
			loading.text = "Loading..." + ((percent != 0)?b:a);
			progress.setDoneValue(percent);
			
			var prt:int = (percent * 2) > 200 ? 200 : percent * 2;
			symicon.gotoAndStop(prt);
			
			if (percent == 100) {
				//TweenMax.delayedCall(1,functionDelay);
			}
		}
		private function functionDelay():void {
			setHide();
			setReset();
		}
		public function setHide():void {
			//back.visible = false;
			this.visible = false;
		}
		public function setShow():void {
			//transformBackground();
			//this.visible = true;
		}
		public function setReset():void {
			var a:String = "0%";
			loading.text = a;
			progress.setDoneValue(0);
		}
	}

}